package com.junzhiit.nengji.cms.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bainaeco.framework.util.DateUtil;
import com.bainaeco.framework.util.serializer.Format;
import com.junzhiit.nengji.bo.AnalysisBO;
import com.junzhiit.nengji.bo.HomeTableBO;
import com.junzhiit.nengji.bo.TransactionAnalysisBO;
import com.junzhiit.nengji.cms.service.TransactionService;
import com.junzhiit.nengji.cms.web.exception.ChainNotFoundException;
import com.junzhiit.nengji.cms.web.vo.HomeTableVO;
import com.junzhiit.nengji.cms.web.vo.OrdersInfoVO;
import com.junzhiit.nengji.cms.web.vo.ResultVO;
import com.junzhiit.nengji.cms.web.vo.SystemUserSessionVO;
import com.junzhiit.nengji.cms.web.vo.TransactionAnalysisVO;
import com.junzhiit.nengji.cms.web.vo.TransactionInfoVO;
import com.junzhiit.nengji.dao.ChainDAO;
import com.junzhiit.nengji.dao.TransactionDAO;
import com.junzhiit.nengji.dao.cache.SystemUserCacheDAO;
import com.junzhiit.nengji.dao.util.DIYKey;
import com.junzhiit.nengji.model.Chain;
import com.junzhiit.nengji.model.enums.AnalysisType;
import com.junzhiit.nengji.model.enums.LoginType;
import com.junzhiit.nengji.model.enums.TransactionAnalysisStatus;

@Service
public class TransactionServiceImpl extends BaseServiceImpl implements TransactionService {

	@Autowired
	private TransactionDAO transactionDAO;
	
	@Autowired
	private SystemUserCacheDAO systemUserCacheDAO;
	
	@Autowired
	private ChainDAO chainDAO;

	@Override
	public TransactionAnalysisVO getTransactionAnalysis(String sessionId,Integer analysisType, String date,String startTime,String endTime) {
		String key = DIYKey.getSessionHashKey(sessionId);
		String shopId=null;
		SystemUserSessionVO session = systemUserCacheDAO.getHash(key, SystemUserSessionVO.class);
		if (LoginType.SHOP.toIntType() == session.getLoginType()) {
			shopId=session.getSystemId();
		}

		String dateStr = null;
		String dateCompareStr = null;
		if (analysisType == AnalysisType.DAY.toIntValue()) {
			dateStr = date;
			dateCompareStr = DateUtil.format(DateUtil.addDays(DateUtil.parseDate(date), -1));
		} else if(analysisType == AnalysisType.MONTH.toIntValue()){
			int year = DateUtil.getYear(DateUtil.parseDate(date));
			int month = DateUtil.getMonth(DateUtil.parseDate(date)) + 1;

			dateStr = year + DateUtil.DATE_DIVISION;
			if (month < 10) {
				dateStr += "0";
			}
			dateStr += month;

			int compareMonth = month - 1;
			int compareYear = year;
			if (compareMonth == 0) {
				compareMonth = 12;
				compareYear = compareYear - 1;
			}
			dateCompareStr = compareYear + DateUtil.DATE_DIVISION;
			if (compareMonth < 10) {
				dateCompareStr += "0";
			}
			dateCompareStr += compareMonth;
		}
		
		TransactionAnalysisVO analysisVO = new TransactionAnalysisVO();
		TransactionAnalysisBO analysisBO=null;
		
		if(analysisType == AnalysisType.DAY.toIntValue()||analysisType == AnalysisType.MONTH.toIntValue()){
			analysisBO = transactionDAO.getTransactionAnalysis(shopId,analysisType,dateStr);
			TransactionAnalysisBO compareAnalysisBO = transactionDAO.getTransactionAnalysis(shopId,analysisType,dateCompareStr);
			
			analysisVO.setSevenRepeatPurchase((analysisBO.getSevenRepeatPurchase()==null)?0:analysisBO.getSevenRepeatPurchase());
			analysisVO.setThirtyRepeatPurchase((analysisBO.getThirtyRepeatPurchase()==null)?0:analysisBO.getThirtyRepeatPurchase());
			analysisVO.setNinetyRepeatPurchase((analysisBO.getNinetyRepeatPurchase()==null)?0:analysisBO.getNinetyRepeatPurchase());

			Double ordersUserCountRate = calaRate(analysisBO.getOrdersUserCount(), compareAnalysisBO.getOrdersUserCount());
			analysisVO.setOrdersUserCountRate(ordersUserCountRate);

			Double ordersCountRate = calaRate(analysisBO.getOrdersCount(), compareAnalysisBO.getOrdersCount());
			analysisVO.setOrdersCountRate(ordersCountRate);

			Double ordersMoneyRate = calaRate(analysisBO.getOrdersMoney(), compareAnalysisBO.getOrdersMoney());
			analysisVO.setOrdersMoneyRate(ordersMoneyRate);

			Double payUserCountRate = calaRate(analysisBO.getPayUserCount(), compareAnalysisBO.getPayUserCount());
			analysisVO.setPayUserCountRate(payUserCountRate);

			Double payOrdersCountRate = calaRate(analysisBO.getPayOrdersCount(), compareAnalysisBO.getPayOrdersCount());
			analysisVO.setPayOrdersCountRate(payOrdersCountRate);

			Double payMoneyRate = calaRate(analysisBO.getPayMoney(), compareAnalysisBO.getPayMoney());
			analysisVO.setPayMoneyRate(payMoneyRate);

			Double payNumberRate = calaRate(analysisBO.getPayNumber(), compareAnalysisBO.getPayNumber());
			analysisVO.setPayNumberRate(payNumberRate);

			Double clientPriceRate = calaRate(analysisBO.getClientPrice(), compareAnalysisBO.getClientPrice());
			analysisVO.setClientPriceRate(clientPriceRate);
			
			Double ordersGoodsCountRate = calaRate(analysisBO.getOrdersGoodsCount(), compareAnalysisBO.getOrdersGoodsCount());
			analysisVO.setOrdersGoodsCountRate(ordersGoodsCountRate);
			
//			Double ordersConversionRate = calaRate(analysisBO.getOrdersConversion(), compareAnalysisBO.getOrdersConversion());
//			analysisVO.setOrdersConversionRate(ordersConversionRate);
//			
//			Double payConversionRate = calaRate(analysisBO.getPayConversion(), compareAnalysisBO.getPayConversion());
//			analysisVO.setPayConversionRate(payConversionRate);
			
			Double paySuccessRate = calaRate(analysisBO.getPaySuccess(), compareAnalysisBO.getPaySuccess());
			analysisVO.setPaySuccessRate(paySuccessRate);
		}else if(analysisType == AnalysisType.CUSTOM.toIntValue()){
			analysisBO = transactionDAO.getTransactionAnalysis(shopId,DateUtil.getDateStart(DateUtil.parseDate(startTime)),DateUtil.getDateEnd(DateUtil.parseDate(endTime)));
		}
		
		analysisVO.setOrdersUserCount((analysisBO.getOrdersUserCount()==null)?0:analysisBO.getOrdersUserCount());
		analysisVO.setOrdersCount((analysisBO.getOrdersCount()==null)?0:analysisBO.getOrdersCount());
		analysisVO.setOrdersMoney((analysisBO.getOrdersMoney()==null)?0.00:Format.decimal(analysisBO.getOrdersMoney()));
		analysisVO.setPayUserCount((analysisBO.getPayUserCount()==null)?0:analysisBO.getPayUserCount());
		analysisVO.setPayOrdersCount((analysisBO.getPayOrdersCount()==null)?0:analysisBO.getPayOrdersCount());
		analysisVO.setPayMoney((analysisBO.getPayMoney()==null)?0.00:Format.decimal(analysisBO.getPayMoney()));
		analysisVO.setPayNumber((analysisBO.getPayNumber()==null)?0:analysisBO.getPayNumber());
		analysisVO.setClientPrice((analysisBO.getClientPrice()==null)?0.00:Format.decimal(analysisBO.getClientPrice()));
		analysisVO.setOrdersGoodsCount((analysisBO.getOrdersGoodsCount()==null)?0:analysisBO.getOrdersGoodsCount());
//		analysisVO.setOrdersConversion((analysisBO.getOrdersConversion()==null)?0.00:Format.decimal(analysisBO.getOrdersConversion()*100));
//		analysisVO.setPayConversion((analysisBO.getPayConversion()==null)?0.00:Format.decimal(analysisBO.getPayConversion()*100));
		analysisVO.setPaySuccess((analysisBO.getPaySuccess()==null)?0.00:Format.decimal(analysisBO.getPaySuccess()*100));
		
		return analysisVO;
	}

	public Double calaRate(Object currentObj, Object compareObj) {
		Double rate = null;
		Double currentNum = null;
		Double compareNum = null;

		currentNum = (currentObj == null) ? 0.00 : Format.decimal(Double.valueOf(currentObj.toString()));

		compareNum = (compareObj == null) ? 0.00 : Format.decimal(Double.valueOf(compareObj.toString()));

		if (currentNum == 0.00 && compareNum == 0.00) {
			rate = 0.00;
		} else if (currentNum == 0.00 && compareNum != 0.00) {
			rate = -100.00;
		} else if (currentNum != 0.00 && compareNum == 0.00) {
			rate = 100.00;
		} else {
			rate = (currentNum - compareNum) / compareNum * 100;
		}

		return Double.valueOf(Format.decimal(rate));
	}

	@Override
	public TransactionInfoVO findAllTransactionAnalysis(String sessionId,Integer analysisType,String date,String startTime,String endTime) {
		String key = DIYKey.getSessionHashKey(sessionId);
		String shopId=null;
		SystemUserSessionVO session = systemUserCacheDAO.getHash(key, SystemUserSessionVO.class);
		if (LoginType.SHOP.toIntType() == session.getLoginType()) {
			shopId=session.getSystemId();
		}

		List<String> dateStrList = getDateStrList(analysisType, date, startTime, endTime);

		// 下单笔数
		List<AnalysisBO> ordersCountList=null;
		// 下单人数
		List<AnalysisBO> ordersUserCountList=null;
		// 下单金额
		List<AnalysisBO> ordersMoneyList=null;
		// 支付订单数
		List<AnalysisBO> payOrdersCountList=null;
		// 支付人数
		List<AnalysisBO> payUserCountList=null;
		// 支付金额
		List<AnalysisBO> payMoneyList=null;

		
		if(analysisType == AnalysisType.DAY.toIntValue()||analysisType == AnalysisType.MONTH.toIntValue()){
			ordersCountList = transactionDAO.findAllTransactionAnalysis(shopId,
					TransactionAnalysisStatus.ORDERSCOUNT.toIntValue(), analysisType, DateUtil.parseDate(date));
			
			ordersUserCountList = transactionDAO
					.findAllTransactionAnalysis(shopId,TransactionAnalysisStatus.ORDERSUSERCOUNT.toIntValue(), analysisType, DateUtil.parseDate(date));
			
			ordersMoneyList = transactionDAO
					.findAllTransactionAnalysis(shopId,TransactionAnalysisStatus.ORDERSMONEY.toIntValue(), analysisType, DateUtil.parseDate(date));
			
			payOrdersCountList = transactionDAO
					.findAllTransactionAnalysis(shopId,TransactionAnalysisStatus.PAYORDERSCOUNT.toIntValue(), analysisType, DateUtil.parseDate(date));
			
			payUserCountList = transactionDAO
					.findAllTransactionAnalysis(shopId,TransactionAnalysisStatus.PAYUSERCOUNT.toIntValue(), analysisType, DateUtil.parseDate(date));
			
			payMoneyList = transactionDAO
					.findAllTransactionAnalysis(shopId,TransactionAnalysisStatus.PAYMONEY.toIntValue(), analysisType, DateUtil.parseDate(date));
			
		}else if(analysisType == AnalysisType.CUSTOM.toIntValue()){
			ordersCountList = transactionDAO.findAllTransactionAnalysis(shopId,
					TransactionAnalysisStatus.ORDERSCOUNT.toIntValue(),DateUtil.getDateStart(DateUtil.parseDate(startTime)),DateUtil.getDateEnd(DateUtil.parseDate(endTime)));

			ordersUserCountList = transactionDAO
					.findAllTransactionAnalysis(shopId,TransactionAnalysisStatus.ORDERSUSERCOUNT.toIntValue(),DateUtil.getDateStart(DateUtil.parseDate(startTime)),DateUtil.getDateEnd(DateUtil.parseDate(endTime)));
			
			ordersMoneyList = transactionDAO
					.findAllTransactionAnalysis(shopId,TransactionAnalysisStatus.ORDERSMONEY.toIntValue(),DateUtil.getDateStart(DateUtil.parseDate(startTime)),DateUtil.getDateEnd(DateUtil.parseDate(endTime)));
			
			payOrdersCountList = transactionDAO
					.findAllTransactionAnalysis(shopId,TransactionAnalysisStatus.PAYORDERSCOUNT.toIntValue(),DateUtil.getDateStart(DateUtil.parseDate(startTime)),DateUtil.getDateEnd(DateUtil.parseDate(endTime)));

			payUserCountList = transactionDAO
					.findAllTransactionAnalysis(shopId,TransactionAnalysisStatus.PAYUSERCOUNT.toIntValue(),DateUtil.getDateStart(DateUtil.parseDate(startTime)),DateUtil.getDateEnd(DateUtil.parseDate(endTime)));

			payMoneyList = transactionDAO
					.findAllTransactionAnalysis(shopId,TransactionAnalysisStatus.PAYMONEY.toIntValue(),DateUtil.getDateStart(DateUtil.parseDate(startTime)),DateUtil.getDateEnd(DateUtil.parseDate(endTime)));
		}

		Map<String, Double> ordersCountMap = ordersCountList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));

		Map<String, Double> ordersUserCountMap = ordersUserCountList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));

		Map<String, Double> ordersMoneyMap = ordersMoneyList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));
		
		Map<String, Double> payOrdersCountMap = payOrdersCountList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));

		Map<String, Double> payUserCountMap = payUserCountList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));

		Map<String, Double> payMoneyMap = payMoneyList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));
		
		List<Double> ordersCount = new ArrayList<Double>();
		List<Double> ordersUserCount = new ArrayList<Double>();
		List<Double> ordersMoney = new ArrayList<Double>();
		List<Double> payOrdersCount = new ArrayList<Double>();
		List<Double> payUserCount = new ArrayList<Double>();
		List<Double> payMoney = new ArrayList<Double>();
		for (String string : dateStrList) {
			if (ordersCountMap.containsKey(string)) {
				ordersCount.add(Format.decimal(ordersCountMap.get(string)));
			} else {
				ordersCount.add(0D);
			}

			if (ordersUserCountMap.containsKey(string)) {
				ordersUserCount.add(Format.decimal(ordersUserCountMap.get(string)));
			} else {
				ordersUserCount.add(0D);
			}

			if (ordersMoneyMap.containsKey(string)) {
				ordersMoney.add(Format.decimal(ordersMoneyMap.get(string)));
			} else {
				ordersMoney.add(0D);
			}
			
			if (payOrdersCountMap.containsKey(string)) {
				payOrdersCount.add(Format.decimal(payOrdersCountMap.get(string)));
			} else {
				payOrdersCount.add(0.00);
			}

			if (payUserCountMap.containsKey(string)) {
				payUserCount.add(Format.decimal(payUserCountMap.get(string)));
			} else {
				payUserCount.add(0.00);
			}

			if (payMoneyMap.containsKey(string)) {
				payMoney.add(Format.decimal(payMoneyMap.get(string)));
			} else {
				payMoney.add(0.00);
			}
		}

		TransactionInfoVO infoVO = new TransactionInfoVO();

		List<Object> data = new ArrayList<Object>();
		data.add(ordersCount);
		data.add(ordersUserCount);
		data.add(ordersMoney);
		data.add(payOrdersCount);
		data.add(payUserCount);
		data.add(payMoney);

		infoVO.setData(data);
		infoVO.setItem(dateStrList);
		
		return infoVO;
	}

	public List<String> getDateStrList(Integer analysisType,String date,String startTime,String endTime){
		List<String> dateStrList = new ArrayList<String>();
		if (analysisType == AnalysisType.DAY.toIntValue()) {
			int n = -29;
			dateStrList.add(date);
			for (int i = -1; i >= n; i--) {
				String dateStr = DateUtil.format(DateUtil.addDays(DateUtil.parseDate(date), i));
				dateStrList.add(dateStr);
			}
		} else if (analysisType == AnalysisType.MONTH.toIntValue()) {
			int year = DateUtil.getYear(DateUtil.parseDate(date));
			int month = DateUtil.getMonth(DateUtil.parseDate(date)) + 1;
			String dateStr = year + DateUtil.DATE_DIVISION;
			if (month < 10) {
				dateStr += "0";
			}
			dateStr += month;
			dateStrList.add(dateStr);
			
			int n = 5;
			for (int i = 1; i <= n; i++) {
				dateStr="";
				month -= 1;
				if (month == 0) {
					month = 12;
					year = year - 1;
				}

				dateStr = year + DateUtil.DATE_DIVISION;
				if (month < 10) {
					dateStr += "0";
				}
				dateStr += month;
				dateStrList.add(dateStr);
			}
		} else if(analysisType == AnalysisType.CUSTOM.toIntValue()){
			String dateStr=endTime;
			while(!dateStr.equals(startTime)){
				dateStrList.add(dateStr);
				dateStr = DateUtil.format(DateUtil.addDays(DateUtil.parseDate(dateStr), -1));
			}
			dateStrList.add(startTime);
		}

		Collections.reverse(dateStrList);
		return dateStrList;
	}

	@Override
	public ResultVO findChainTransaction(HomeTableVO homeTableVO) {
		ResultVO result = new ResultVO();
		if(homeTableVO.getYear()==null||homeTableVO.getMonth()==null){
			return result;
		}
		
		String key = DIYKey.getSessionHashKey(homeTableVO.getSessionId());
		SystemUserSessionVO session = systemUserCacheDAO.getHash(key, SystemUserSessionVO.class);
		HomeTableBO homeTableBO = new HomeTableBO();
		BeanUtils.copyProperties(homeTableVO, homeTableBO);
		homeTableBO.setChainId(session.getSystemId());
		
		// 下单笔数
		homeTableBO.setStatus(TransactionAnalysisStatus.ORDERSCOUNT.toIntValue());
		List<AnalysisBO> ordersCountList=transactionDAO.findChainOrdersTransaction(homeTableBO);
		// 下单人数
		homeTableBO.setStatus(TransactionAnalysisStatus.ORDERSUSERCOUNT.toIntValue());
		List<AnalysisBO> ordersUserCountList=transactionDAO.findChainOrdersTransaction(homeTableBO);
		// 下单金额
		homeTableBO.setStatus(TransactionAnalysisStatus.ORDERSMONEY.toIntValue());
		List<AnalysisBO> ordersMoneyList=transactionDAO.findChainOrdersTransaction(homeTableBO);
		// 支付订单数
		homeTableBO.setStatus(TransactionAnalysisStatus.PAYORDERSCOUNT.toIntValue());
		List<AnalysisBO> payOrdersCountList=transactionDAO.findChainPayTransaction(homeTableBO);
		// 支付人数
		homeTableBO.setStatus(TransactionAnalysisStatus.PAYUSERCOUNT.toIntValue());
		List<AnalysisBO> payUserCountList=transactionDAO.findChainPayTransaction(homeTableBO);
		// 支付金额
		homeTableBO.setStatus(TransactionAnalysisStatus.PAYMONEY.toIntValue());
		List<AnalysisBO> payMoneyList=transactionDAO.findChainPayTransaction(homeTableBO);
		
		Map<String, Double> ordersCountMap = ordersCountList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));
		

		Map<String, Double> ordersUserCountMap = ordersUserCountList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));

		Map<String, Double> ordersMoneyMap = ordersMoneyList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));
		
		Map<String, Double> payOrdersCountMap = payOrdersCountList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));

		Map<String, Double> payUserCountMap = payUserCountList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));

		Map<String, Double> payMoneyMap = payMoneyList.stream()
				.collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));
		
		List<String> item=new ArrayList<String>();
		List<Double> ordersCount = new ArrayList<Double>();
		List<Double> ordersUserCount = new ArrayList<Double>();
		List<Double> ordersMoney = new ArrayList<Double>();
		List<Double> payOrdersCount = new ArrayList<Double>();
		List<Double> payUserCount = new ArrayList<Double>();
		List<Double> payMoney = new ArrayList<Double>();
		
		Calendar cl=Calendar.getInstance();
		cl.set(Calendar.MONTH,homeTableBO.getMonth()-1);
		int daysCount=cl.getActualMaximum(Calendar.DATE);
		
		String prefixStr=String.valueOf(homeTableBO.getYear())+"-"+String.valueOf(homeTableBO.getMonth())+"-";
		for(int i=1;i<=daysCount;i++){
			String index=String.valueOf(i);
			item.add(prefixStr+index);
			
			if(ordersCountMap.containsKey(index)){
				ordersCount.add(ordersCountMap.get(index));
			}else{
				ordersCount.add(0.00);
			}
			
			if(ordersUserCountMap.containsKey(index)){
				ordersUserCount.add(ordersUserCountMap.get(index));
			}else{
				ordersUserCount.add(0.00);
			}
			
			if(ordersMoneyMap.containsKey(index)){
				ordersMoney.add(ordersMoneyMap.get(index));
			}else{
				ordersMoney.add(0.00);
			}
			
			if(payOrdersCountMap.containsKey(index)){
				payOrdersCount.add(payOrdersCountMap.get(index));
			}else{
				payOrdersCount.add(0.00);
			}
			if(payUserCountMap.containsKey(index)){
				payUserCount.add(payUserCountMap.get(index));
			}else{
				payUserCount.add(0.00);
			}
			if(payMoneyMap.containsKey(index)){
				payMoney.add(payMoneyMap.get(index));
			}else{
				payMoney.add(0.00);
			}
		}
		
		OrdersInfoVO ordersInfoVO=new OrdersInfoVO();
		
		List<Object> data=new ArrayList<Object>();
		data.add(ordersCount);
		data.add(ordersUserCount);
		data.add(ordersMoney);
		data.add(payOrdersCount);
		data.add(payUserCount);
		data.add(payMoney);
		
		ordersInfoVO.setData(data);
		ordersInfoVO.setItem(item);
		
		result.setData(ordersInfoVO);
		
		return result;
	}

	@Override
	public TransactionAnalysisVO getChainTransactionAnalysis(String sessionId, Integer analysisType, String date,String startTime, String endTime, String shopId) {
		SystemUserSessionVO sessionVO=systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		String chainId=sessionVO.getSystemId();
		Chain chain=chainDAO.query(chainId);
		if(chain==null){
			throw new ChainNotFoundException("品牌不存在");
		}
		String currentDate=null;
		String preDate=null;
		if(analysisType==AnalysisType.DAY.toIntValue()){
			currentDate=date;
			preDate=DateUtil.format(DateUtil.addDays(DateUtil.parseDate(date),-1));
		}
		if(analysisType==AnalysisType.MONTH.toIntValue()){
			Date cDate=DateUtil.parseDate(date);
			Integer year=DateUtil.getYear(cDate);
			Integer month=DateUtil.getMonth(cDate)+1;
			currentDate=year+DateUtil.DATE_DIVISION;
			if(month<10){
				currentDate+="0";
			}
			currentDate+=month;
			Integer preMonth=month-1;
			if(preMonth==0){
				preMonth=12;
				year-=1;
			}
			preDate=year+DateUtil.DATE_DIVISION;
			if(preMonth<10){
				preDate+="0";
			}
			preDate+=preMonth;
		}
		TransactionAnalysisBO tab=transactionDAO.getChainTransactionAnalysis(shopId, DateUtil.getDateStart(DateUtil.parseDate(startTime)),DateUtil.getDateEnd(DateUtil.parseDate(endTime)), chainId, currentDate, analysisType);
		TransactionAnalysisVO tav=new TransactionAnalysisVO();
		BeanUtils.copyProperties(tab, tav);
		tav.setOrdersGoodsCount(tav.getOrdersGoodsCount()==null?0:tav.getOrdersGoodsCount());;
		tav.setOrdersMoney(tav.getOrdersMoney()==null?0:tav.getOrdersMoney());
		tav.setPayMoney(tav.getPayMoney()==null?0:tav.getPayMoney());
		tav.setPayNumber(tav.getPayNumber()==null?0:tav.getPayNumber());
		tav.setOrdersConversion(tav.getOrdersConversion()==null?0:tav.getOrdersConversion());
		tav.setPayConversion(tav.getPayConversion()==null?0:tav.getPayConversion());
		tav.setPaySuccess(tav.getPaySuccess()==null?0:tav.getPaySuccess());
		tav.setClientPrice(tav.getClientPrice()==null?0:tav.getClientPrice());
		tav.setSevenRepeatPurchase(tav.getSevenRepeatPurchase()==null?0:tav.getSevenRepeatPurchase());
		tav.setThirtyRepeatPurchase(tav.getThirtyRepeatPurchase()==null?0:tav.getThirtyRepeatPurchase());
		tav.setNinetyRepeatPurchase(tav.getNinetyRepeatPurchase()==null?0:tav.getNinetyRepeatPurchase());
		TransactionAnalysisBO preTab=transactionDAO.getChainTransactionAnalysis(shopId, DateUtil.getDateStart(DateUtil.parseDate(startTime)), DateUtil.getDateEnd(DateUtil.parseDate(endTime)), chainId, preDate, analysisType);
		Double oucr=calaRate(tab.getOrdersUserCount(), preTab.getOrdersUserCount());
		tav.setOrdersUserCountRate(oucr);
		Double ocr=calaRate(tab.getOrdersCount(), preTab.getOrdersCount());
		tav.setOrdersCountRate(ocr);
		Double omr=calaRate(tab.getOrdersMoney(), preTab.getOrdersMoney());
		tav.setOrdersMoneyRate(omr);
		Double pucr=calaRate(tab.getPayUserCount(), preTab.getPayUserCount());
		tav.setPayUserCountRate(pucr);
		Double pocr=calaRate(tab.getPayOrdersCount(), preTab.getPayOrdersCount());
		tav.setPayOrdersCountRate(pocr);
		Double pmr=calaRate(tab.getPayMoney(),preTab.getPayMoney());
		tav.setPayMoneyRate(pmr);
		Double pnr=calaRate(tab.getPayNumber(), preTab.getPayNumber());
		tav.setPayNumberRate(pnr);
		Double cpr=calaRate(tab.getClientPrice(),preTab.getClientPrice());
		tav.setClientPriceRate(cpr);
		Double ogcr=calaRate(tab.getOrdersGoodsCount(), preTab.getOrdersGoodsCount());
		tav.setOrdersGoodsCountRate(ogcr);
		Double ocvr=calaRate(tab.getOrdersConversion(), preTab.getOrdersConversion());
		tav.setOrdersConversionRate(ocvr);
		Double pcr=calaRate(tab.getPayConversion(), preTab.getPayConversion());
		tav.setPayConversionRate(pcr);
		Double psr=calaRate(tab.getPaySuccess(), preTab.getPaySuccess());
		tav.setPaySuccessRate(psr);
		return tav;
	}

	@Override
	public TransactionInfoVO findAllChainTransactionAnalysis(String sessionId, Integer analysisType, String date,String startTime, String endTime, String shopId) {
		SystemUserSessionVO sessionVO=systemUserCacheDAO.getHash(DIYKey.getSessionHashKey(sessionId), SystemUserSessionVO.class);
		String chainId=sessionVO.getSystemId();
		Chain chain=chainDAO.query(chainId);
		if(chain==null){
			throw new ChainNotFoundException("品牌不存在");
		}
		Date sTime=DateUtil.getDateStart(DateUtil.parseDate(startTime));
		Date eTime=DateUtil.getDateEnd(DateUtil.parseDate(endTime));
		Integer status=TransactionAnalysisStatus.ORDERSCOUNT.toIntValue();
		List<AnalysisBO> ocList=transactionDAO.findAllChainTransactionAnalysis(shopId, status, analysisType, date, chainId, sTime, eTime);
		Map<String,Double> ocMap=ocList.stream().collect(Collectors.toMap(AnalysisBO::getDate,AnalysisBO::getValue));
		status=TransactionAnalysisStatus.PAYMONEY.toIntValue();
		List<AnalysisBO> pmList=transactionDAO.findAllChainTransactionAnalysis(shopId, status, analysisType, date, chainId, sTime, eTime);
		Map<String,Double> pmMap=pmList.stream().collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));
		status=TransactionAnalysisStatus.ORDERSUSERCOUNT.toIntValue();
		List<AnalysisBO> oucList=transactionDAO.findAllChainTransactionAnalysis(shopId, status, analysisType, date, chainId, sTime, eTime);
		Map<String,Double> oucMap=oucList.stream().collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));
		status=TransactionAnalysisStatus.ORDERSMONEY.toIntValue();
		List<AnalysisBO> omList=transactionDAO.findAllChainTransactionAnalysis(shopId, status, analysisType, date, chainId, sTime, eTime);
		Map<String,Double> omMap=omList.stream().collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));
		status=TransactionAnalysisStatus.PAYUSERCOUNT.toIntValue();
		List<AnalysisBO> pucList=transactionDAO.findAllChainTransactionAnalysis(shopId, status, analysisType, date, chainId, sTime, eTime);
		Map<String,Double> pucMap=pucList.stream().collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));
		status=TransactionAnalysisStatus.PAYORDERSCOUNT.toIntValue();
		List<AnalysisBO> pocList=transactionDAO.findAllChainTransactionAnalysis(shopId, status, analysisType, date, chainId, sTime, eTime);
		Map<String,Double> pocMap=pocList.stream().collect(Collectors.toMap(AnalysisBO::getDate, AnalysisBO::getValue));
		List<Double> ocCountList=new ArrayList<>();
		List<Double> pmCountList=new ArrayList<>();
		List<Double> oucCountList=new ArrayList<>();
		List<Double> omCountList=new ArrayList<>();
		List<Double> pucCountList=new ArrayList<>();
		List<Double> pocCountList=new ArrayList<>();
		List<String> dateStr=getDateStrList(analysisType, date, startTime, endTime);
		for(String index:dateStr){
			if(ocMap.containsKey(index)){
				ocCountList.add(ocMap.get(index));
			}else{
				ocCountList.add(0D);
			}
			if(pmMap.containsKey(index)){
				pmCountList.add(pmMap.get(index));
			}else{
				pmCountList.add(0D);
			}
			if(oucMap.containsKey(index)){
				oucCountList.add(oucMap.get(index));
			}else{
				oucCountList.add(0D);
			}
			if(omMap.containsKey(index)){
				omCountList.add(omMap.get(index));
			}else{
				omCountList.add(0D);
			}
			if(pucMap.containsKey(index)){
				pucCountList.add(pucMap.get(index));
			}else{
				pucCountList.add(0D);
			}
			if(pocMap.containsKey(index)){
				pocCountList.add(pocMap.get(index));
			}else{
				pocCountList.add(0D);
			}
		}
		List<Object> data=new ArrayList<>();
		data.add(ocCountList);
		data.add(oucCountList);
		data.add(omCountList);
		data.add(pocCountList);
		data.add(pucCountList);
		data.add(pmCountList);
		TransactionInfoVO tiv=new TransactionInfoVO();
		tiv.setData(data);
		tiv.setItem(dateStr);
		return tiv;
	}
	
}
